/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.soap.mail.type.Policy;
/*     */ import com.zimbra.soap.mail.type.RetentionPolicy;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SetRetentionPolicy
/*     */   extends RedoableOp
/*     */ {
/*  36 */   private int itemId = -1;
/*     */   private RetentionPolicy retentionPolicy;
/*     */   private MailItem.Type type;
/*     */   
/*     */   public SetRetentionPolicy() {
/*  41 */     super(MailboxOperation.SetRetentionPolicy);
/*     */   }
/*     */   
/*     */   public SetRetentionPolicy(int mailboxId, MailItem.Type type, int itemId, RetentionPolicy rp) {
/*  45 */     this();
/*  46 */     setMailboxId(mailboxId);
/*  47 */     this.type = type;
/*     */     
/*  49 */     this.retentionPolicy = rp;
/*     */   }
/*     */   
/*     */   protected String getPrintableData() {
/*  53 */     return "retentionPolicy=" + this.retentionPolicy;
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException {
/*  57 */     List<Policy> keepPolicy = this.retentionPolicy.getKeepPolicy();
/*  58 */     List<Policy> purgePolicy = this.retentionPolicy.getPurgePolicy();
/*     */     
/*  60 */     out.writeByte(this.type.toByte());
/*  61 */     out.writeInt(this.itemId);
/*  62 */     out.writeInt(keepPolicy.size());
/*  63 */     for (Policy policy : keepPolicy) {
/*  64 */       out.writeUTF(policy.getId());
/*  65 */       out.writeUTF(policy.getLifetime());
/*     */     }
/*  67 */     out.writeInt(purgePolicy.size());
/*  68 */     for (Policy policy : purgePolicy) {
/*  69 */       out.writeUTF(policy.getId());
/*  70 */       out.writeUTF(policy.getLifetime());
/*     */     }
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException {
/*  75 */     this.type = MailItem.Type.of(in.readByte());
/*  76 */     if ((this.type != MailItem.Type.FOLDER) && (this.type != MailItem.Type.TAG)) {
/*  77 */       throw new IOException("Unexpected item type: " + this.type);
/*     */     }
/*  79 */     this.itemId = in.readInt();
/*  80 */     int size = in.readInt();
/*  81 */     List<Policy> keep = readPolicyList(in, size);
/*  82 */     size = in.readInt();
/*  83 */     List<Policy> purge = readPolicyList(in, size);
/*  84 */     this.retentionPolicy = new RetentionPolicy(keep, purge);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private List<Policy> readPolicyList(RedoLogInput in, int size)
/*     */     throws IOException
/*     */   {
/*  92 */     List<Policy> list = Lists.newArrayList();
/*  93 */     for (int i = 1; i <= size; i++) {
/*  94 */       String id = in.readUTF();
/*  95 */       String lifetime = in.readUTF();
/*     */       Policy p;
/*  97 */       Policy p; if (id != null) {
/*  98 */         p = Policy.newSystemPolicy(id);
/*     */       } else {
/* 100 */         p = Policy.newUserPolicy(lifetime);
/*     */       }
/* 102 */       list.add(p);
/*     */     }
/* 104 */     return list;
/*     */   }
/*     */   
/*     */   public void redo() throws Exception
/*     */   {
/* 109 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(getMailboxId());
/* 110 */     if (this.type == MailItem.Type.FOLDER) {
/* 111 */       mbox.setRetentionPolicy(getOperationContext(), this.itemId, MailItem.Type.FOLDER, this.retentionPolicy);
/*     */     } else {
/* 113 */       mbox.setRetentionPolicy(getOperationContext(), this.itemId, MailItem.Type.TAG, this.retentionPolicy);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   int getItemId()
/*     */   {
/* 120 */     return this.itemId;
/*     */   }
/*     */   
/*     */   public RetentionPolicy getRetentionPolicy() {
/* 124 */     return this.retentionPolicy;
/*     */   }
/*     */   
/*     */   byte[] testSerialize() throws IOException {
/* 128 */     ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 129 */     serializeData(new RedoLogOutput(out));
/* 130 */     return out.toByteArray();
/*     */   }
/*     */   
/*     */   void testDeserialize(byte[] data) throws IOException {
/* 134 */     deserializeData(new RedoLogInput(new ByteArrayInputStream(data)));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/SetRetentionPolicy.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */